module.exports = function (grunt) {
    // load plugins
    [
        'grunt-cafe-mocha',
        'grunt-contrib-jshint',
        'grunt-exec',
        'grunt-contrib-sass',
        'grunt-contrib-uglify',
        'grunt-contrib-cssmin',
        'grunt-hashres',
        'grunt-lint-pattern'
    ].forEach(function (task) {
            grunt.loadNpmTasks(task);
        });

// configure plugins
    grunt.initConfig({
        cafemocha: {
            all: {src: 'qa/tests-*.js', options: {ui: 'tdd', timeout: 2000}}
        },
        jshint: {
            app: ['meadowlark.js', 'public/js/**/*.js', 'lib/**/*.js'],
            qa: ['Gruntfile.js', 'public/qa/**/*.js', 'qa/**/*.js']
        },
        exec: {
            linkchecker: {cmd: 'linkchecker http://localhost:3000'}
        },
        sass: {
            dist: {
                options: {
                    style: 'expanded'
                },
                files: {
                    'public/css/main.css': 'sass/main.scss',
                    'public/css/cart.css': 'sass/cart.scss'
                }
            }
        },
        uglify: {
            all: {
                files: {
                    'public/js/meadowlark.min.js': ['public/js/**/*.js']
                }
            }
        },
        cssmin: {
            combine: {
                files: {
                    'public/css/meadowlark.css': ['public/css/**/*.css', '!public/css/meadowlark*.css']
                }
            },
            minify: {
                src: 'public/css/meadowlark.css',
                dest: 'public/css/meadowlark.min.css'
            }
        },
        hashres: {
            options: {
                fileNameFormat: '${name}.${hash}.${ext}'
            },
            all: {
                src: ['public/js/meadowlark.min.js', 'public/css/meadowlark.min.css'],
                //dest: ['views/layouts/main.hbs']
                dest: [
                    'config.js'
                ]
            }
        },
        lint_pattern: {
            view_statics: {
                options: {
                    rules: [
                        {
                            pattern: /<link [^>]*href=["'](?!\{\{|(https?:)?\/\/)/,
                            message: 'Un-mapped static resource found in <link>.'
                        },
                        {
                            pattern: /<script [^>]*src=["'](?!\{\{|(https?:)?\/\/)/,
                            message: 'Un-mapped static resource found in <script>.'
                        },
                        {
                            pattern: /<img [^>]*src=["'](?!\{\{|(https?:)?\/\/)/,
                            message: 'Un-mapped static resource found in <img>.'
                        },
                    ]
                },
                files: {
                    src: ['views/**/*.hbs']
                }
            },
            css_statics: {
                options: {
                    rules: [
                        {
                            pattern: /url\(/,
                            message: 'Un-mapped static found in SASS property.'
                        },
                    ]
                },
                files: {
                    src: [
                        'sass/**/*.scss', '!sass/_common.scss'
                    ]
                }
            }
        }
    });

// register tasks
    grunt.registerTask('default', ['cafemocha', 'jshint', 'exec', 'lint_pattern']);
    grunt.registerTask('static', ['sass', 'cssmin', 'uglify', 'hashres']);
};